package com.platform.qujia.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.platform.qujia.dao.OilEngineMapper;
import com.platform.qujia.enums.OilEngineStatusEnum;
import com.platform.qujia.pojo.Enterprise;
import com.platform.qujia.pojo.OilEngine;
import com.platform.qujia.service.EnterpriseService;
import com.platform.qujia.service.OilEngineService;
import com.platform.qujia.utils.DateUtils;
import com.platform.qujia.utils.StringUtils;
import com.platform.qujia.utils.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service("oilEngineService")
@Slf4j
public class OilEngineServiceImpl implements OilEngineService {

    @Resource
    private OilEngineMapper oilEngineMapper;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public int saveOilEngine(OilEngine oilEngine) {
        if(Objects.isNull(oilEngine)){
            throw new CustomException("油机数据录入信息参数为空");
        }
        if(Objects.nonNull(oilEngine.getEnterpriseId())) {
            Enterprise enterprise = enterpriseService.selectEnterpriseInfo(oilEngine.getEnterpriseId().intValue());
            if(Objects.nonNull(enterprise)){
                oilEngine.setEnterpriseName(enterprise.getEnterpriseName());
            }
        }

        int i = oilEngineMapper.insertSelective(oilEngine);
        return i;
    }

    @Override
    public List<OilEngine> selectOilEngineList(OilEngine oilEngine) {
        // 获取10分钟内的订单
        oilEngine.setBeginTime(DateUtils.beforeThirtyMinutes(10));
        return oilEngineMapper.selectOilEngineList(oilEngine);
    }

    @Override
    public int updateOilEngine(OilEngine oilEngine) {
        if(Objects.isNull(oilEngine) || Objects.isNull(oilEngine.getId())){
            throw new CustomException("修改油机订单失败");
        }
        return oilEngineMapper.updateByPrimaryKeySelective(oilEngine);
    }

    @Override
    public OilEngine selectOilEngineById(Long id) {
        return oilEngineMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateOilEngineCallBack(String orderNo) {
        // 修改油机订单状态
        String id = redisTemplate.opsForValue().get(orderNo);
        log.info("redis中油机订单，[{}],订单号：[{}]", id, orderNo);
        if (Objects.nonNull(id)) {
            OilEngine oilEngine = new OilEngine();
            oilEngine.setId(Long.valueOf(id));
            oilEngine.setStatus(OilEngineStatusEnum.PAYMENT.getValue());
            oilEngine.setPayTime(new Date());
            oilEngine.setUpdateTime(new Date());
            oilEngine.setOrderNo(orderNo);
            OilEngine result = oilEngineMapper.selectByPrimaryKey(Long.valueOf(id));
            if(Objects.isNull(result)){
                log.info("查询不到油机订单，[{}],订单号：[{}]", id, orderNo);
            }
            log.info("查询油机订单信息，[{}],订单号：[{}]", JSONObject.toJSONString(result), orderNo);
            if(OilEngineStatusEnum.PAYMENT.getValue().equals(result.getStatus())){
                if(StringUtils.isEmpty(result.getOrderNo())){
                    OilEngine engine = new OilEngine();
                    engine.setId(Long.valueOf(id));
                    engine.setOrderNo(orderNo);
                    int i = updateOilEngine(oilEngine);
                    log.info("增加油机订单号结果：[{}],油机id: [{}],订单号：[{}]", i, id, orderNo);
                    if (i != 1) {
                        log.info("增加油机订单号失败：[{}]，订单号：[{}]", JSONObject.toJSONString(oilEngine), orderNo);
                    }
                }
                log.info("该订单已经同步状态，不需要重复更新，[{}],订单号：[{}]", JSONObject.toJSONString(result),orderNo);
            }else {
                log.info("修改油机订单状态：[{}]，订单号：[{}]", JSONObject.toJSONString(oilEngine), orderNo);
                int i = updateOilEngine(oilEngine);
                log.info("修改油机订单状态结果：[{}],油机id: [{}],订单号：[{}]", i, id, orderNo);
                if (i != 1) {
                    log.info("修改油机订单失败：[{}]，订单号：[{}]", JSONObject.toJSONString(oilEngine), orderNo);
                }
            }
        }
    }
}
